package com.incognito.spur.map.parser;

import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.incognito.spur.map.TiledMap;
import com.incognito.spur.map.TiledMapException;
import com.incognito.spur.map.renderer.TiledMapRenderer;

public class MapParser{

	/**
	 * Parses the given file and returns the generated
	 * Map object.
	 * 
	 * @param mapIs
	 * 		An InputStream to the map file.
	 * @param mapTilsetIs
	 * 		An InputStream to the map tileset file.
	 * @param rendererClass
	 * 		The TiledMapRenderer implementation class to use for this map.
	 * @return
	 * 		The generated Map object.
	 */
	@SuppressWarnings("rawtypes")
	public static TiledMap createMap(InputStream mapIs, Class rendererClass) throws TiledMapException{
		
		TiledMap map = new TiledMap();
		
		try{
			
			TiledMapRenderer renderer = (TiledMapRenderer)rendererClass.newInstance();
			map.setRenderer(renderer);
			
			MapParserHandler handler = new MapParserHandler(map);
		
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			saxParser.parse(mapIs, handler);
			
		}catch(Exception e){
			throw new TiledMapException(e);
		}
		
		return map;
	}
}
